import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import ElementUI from '../components/ElementUI.vue'
import Home from'../components/Home.vue'
import user from"../components/user/user.vue"
import role from"../components/user/role.vue"
import item from"../components/items/item"
import item2 from"../components/items/item2.vue"
import Item3 from"../components/items/Item3.vue"
import ItemCat from"../components/items/ItemCat.vue"

import message from"../components/items/message.vue"
import shoucang from"../components/items/shoucang.vue"
import kecheng from"../components/items/kecheng.vue"
import kechengaddItem from"../components/items/kechengaddItem.vue"

//使用路由机制
Vue.use(VueRouter)
const routes = [
  {path: '/', redirect: '/login'},
  {path: '/login', component: Login},
  {path: '/elementUI', component: ElementUI},
  {path:"/home",component: Home,children:[
    {path:"/user",component: user},
    {path:"/item",component: item},
    {path:"/item2",component:item2},
    {path:"/Item3",component:Item3},
    {path:"/role",component: role},
    {path:"/ItemCat",component: ItemCat},

    {path:"/message",component: message},
    {path:"/shoucang",component: shoucang},
    {path:"/kecheng",component: kecheng},
    {path:"/kechengaddItem",component: kechengaddItem},
  ]}
]
/* 定义路由的步骤


 */
//路由导航守卫!!!!!!!

const router = new VueRouter({
  routes
})
//路由导航守卫!!!!!!!
router.beforeEach((to,from,next)=>{

  if(to.path === '/login') return next()
 let token = window.sessionStorage.getItem("token")
 if(token === null || token ===""){
    return next("/login")
 }
 next()
})
// router.beforeEach((to,from,next)=>{
//   /* 验证有无登录的问题，如果有 就放行*/
//   if(to.path="/login")return next()
//   /* 接下来判断密钥有无的问题*/
//   /* 获取密钥*/
//   let token = window.sessionStorage.getItem("token")
//   /* 判断密钥是否为空*/
//   if(token===null || token === ""){
//       return("/login")
//   }
//   next()
// })

// router.beforeEach((to,from,next)=>{
//   /* 判断有无登录*/
//   if(to.path="/login")return next()
//   /* 接下来判断密钥有无的问题*/
//   /* 获取密钥*/
//   let token = window.sessionStorage.getItem("token")
//   if(token===null||token===""){
//     return("/login")
//   }
//   next()
// })
export default router
